package com.cennavi.minenavidemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.cennavi.minenavidemo.R;
import com.minedata.minenavi.SDKInitializer;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.mapdal.BaseUrl;
import com.minedata.minenavi.mapdal.CoordType;
import com.minedata.minenavi.navi.ExpandView3;

public class SettingActivity extends Activity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private EditText mEtRouteingUrl;
    private EditText mEtTruckUrl;
    private EditText mEtExpandViewUrl;
    private EditText mEtRouteTmcUrl;
    private EditText mEtSearchUrl;
    private EditText mEtInverseUrl;
    private EditText mEtStyleUrl;
    private EditText mEtDatastoreUrl;
    private EditText mEtMonitorUrl;
    private EditText mEtServerHost;
    private EditText mEtServerScheme;
    private EditText mEtAppKey;

    private RadioGroup mRgExpandViewType;
    private RadioGroup mRgMapType;
    private RadioGroup mRgCoordType;
    private RadioGroup mRgLocationType;

    private Button mBtnGenerate;

    private int mExpandViewType = 0;
    private int mMapType = 0;
    private int mCoordType = 0;
    private int mLocationType = 0;

    private SharedPreferences mSharedPreferences;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_set);
        mContext = this;
        // 初始化UI控件
        initView();
    }

    private void initBaseUrl(){
        //南京联创，特勤局项目 测试
        BaseUrl.RouteingUrl = "http://192.168.17.27:80/service/lbs/route/nc/v1/routing";
        BaseUrl.TruckUrl = "http://192.168.17.27:80/service/lbs/nc/v1/truck/routing";
        BaseUrl.ExpandViewUrl = "http://192.168.17.27:80/service/lbs/nc/v1/jv/roadnet";
        BaseUrl.SearchUrl = "http://192.168.17.27:80/service/lbs/";
        BaseUrl.InverseUrl = "http://192.168.17.27:80/service/lbs/";
        BaseUrl.RouteTmcUrl = "http://192.168.17.27:80/service/lbs/nc/v1/nav2tmc";
        BaseUrl.DataStoreUrl = "http://192.168.17.27:80/service/lbs/nc/v1/datastore/006000000/";
        BaseUrl.StyleUrl = "http://192.168.17.27:80/service/lbs/nc/v1/tile/v2";
        BaseUrl.MonitorUrl = "http://192.168.17.27:80/platform";

        SDKInitializer.setApiKey("61e4607995b0410c88db06ff4146571f");
        SDKInitializer.setServerHost("192.168.17.27:80/service/lbs");
        SDKInitializer.setServerScheme("http");

    }



    private void initView() {
        mEtRouteingUrl = findViewById(R.id.et_routeing_url_input);
        SpannableString sRouteingUrl = new SpannableString(BaseUrl.RouteingUrl);
        mEtRouteingUrl.setText(sRouteingUrl);

        mEtTruckUrl = findViewById(R.id.et_truck_url_input);
        SpannableString sTruckUrl = new SpannableString(BaseUrl.TruckUrl);
        mEtTruckUrl.setText(sTruckUrl);

        mEtExpandViewUrl = findViewById(R.id.et_expandview_url_input);
        SpannableString sExpandViewUrl = new SpannableString(BaseUrl.ExpandViewUrl);
        mEtExpandViewUrl.setText(sExpandViewUrl);

        mEtRouteTmcUrl = findViewById(R.id.et_routetmc_url_input);
        SpannableString sRouteTmcUrl = new SpannableString(BaseUrl.RouteTmcUrl);
        mEtRouteTmcUrl.setText(sRouteTmcUrl);

        mEtSearchUrl = findViewById(R.id.et_search_url_input);
        SpannableString sSearchUrl = new SpannableString(BaseUrl.SearchUrl);
        mEtSearchUrl.setText(sSearchUrl);

        mEtInverseUrl = findViewById(R.id.et_inverse_url_input);
        SpannableString sInverseUrl = new SpannableString(BaseUrl.InverseUrl);
        mEtInverseUrl.setText(sInverseUrl);

        mEtStyleUrl = findViewById(R.id.et_style_url_input);
        SpannableString sStyleUrl = new SpannableString(BaseUrl.StyleUrl);
        mEtStyleUrl.setText(sStyleUrl);

        mEtDatastoreUrl = findViewById(R.id.et_datastore_url_input);
        SpannableString sDatastoreUrl = new SpannableString(BaseUrl.DataStoreUrl);
        mEtDatastoreUrl.setText(sDatastoreUrl);

        mEtMonitorUrl = findViewById(R.id.et_monitor_url_input);
        SpannableString sMonitorUrl = new SpannableString(BaseUrl.MonitorUrl);
        mEtMonitorUrl.setText(sMonitorUrl);

        mEtServerHost = findViewById(R.id.et_host_input);

        if (SDKInitializer.getServerHost().equals("mineservice.minedata.cn")) {
            SDKInitializer.setServerHost("mineservice.minedata.cn/service/lbs");
        }

        SpannableString sServerHost = new SpannableString(SDKInitializer.getServerHost());
        mEtServerHost.setText(sServerHost);

        mEtServerScheme = findViewById(R.id.et_scheme_input);
        SpannableString sServerScheme = new SpannableString(SDKInitializer.getServerScheme());
        mEtServerScheme.setText(sServerScheme);

        mEtAppKey = findViewById(R.id.et_appkey_input);

        if (SDKInitializer.getApiKey() != null) {
            SpannableString sAppKey = new SpannableString(SDKInitializer.getApiKey());
            mEtAppKey.setText(sAppKey);
        }

        mRgExpandViewType = findViewById(R.id.rg_expandview_type);
        mRgExpandViewType.setOnCheckedChangeListener(this);

        if (SDKInitializer.getExpandViewUrlType() == ExpandView3.UrlType.roadnet) {
            mRgExpandViewType.check(R.id.rb_roadnet);
        } else {
            mRgExpandViewType.check(R.id.rb_realview);
        }

        mRgMapType = findViewById(R.id.rg_map_type);
        mRgMapType.setOnCheckedChangeListener(this);

        if (SDKInitializer.getStyleUrlType() == MineMap.UrlType.basicMap) {
            mRgMapType.check(R.id.rb_basicmap);
        } else {
            mRgMapType.check(R.id.rb_satellite);
        }

        mRgCoordType = findViewById(R.id.rg_coord_type);
        mRgCoordType.setOnCheckedChangeListener(this);

        if (SDKInitializer.getCoordType() == CoordType.GCJ02) {
            mRgCoordType.check(R.id.rb_02);
        } else {
            mRgCoordType.check(R.id.rb_84);
        }

        mRgLocationType = findViewById(R.id.rg_location_type);
        mRgLocationType.setOnCheckedChangeListener(this);

        if (!SDKInitializer.isUseSimLocation()) {
            mRgLocationType.check(R.id.rb_gps);
        } else {
            mRgLocationType.check(R.id.rb_sim);
        }

        mBtnGenerate = findViewById(R.id.btn_generate);
        mBtnGenerate.setOnClickListener(this);
    }

    private void setData() {
        SpannableString sRouteingUrl = new SpannableString(BaseUrl.RouteingUrl);
        mEtRouteingUrl.setText(sRouteingUrl);
        SpannableString sTruckUrl = new SpannableString(BaseUrl.TruckUrl);
        mEtTruckUrl.setText(sTruckUrl);
        SpannableString sExpandViewUrl = new SpannableString(BaseUrl.ExpandViewUrl);
        mEtExpandViewUrl.setText(sExpandViewUrl);
        SpannableString sRouteTmcUrl = new SpannableString(BaseUrl.RouteTmcUrl);
        mEtRouteTmcUrl.setText(sRouteTmcUrl);
        SpannableString sSearchUrl = new SpannableString(BaseUrl.SearchUrl);
        mEtSearchUrl.setText(sSearchUrl);
        SpannableString sInverseUrl = new SpannableString(BaseUrl.InverseUrl);
        mEtInverseUrl.setText(sInverseUrl);
        SpannableString sStyleUrl = new SpannableString(BaseUrl.StyleUrl);
        mEtStyleUrl.setText(sStyleUrl);
        SpannableString sDatastoreUrl = new SpannableString(BaseUrl.DataStoreUrl);
        mEtDatastoreUrl.setText(sDatastoreUrl);
        SpannableString sMonitorUrl = new SpannableString(BaseUrl.MonitorUrl);
        mEtMonitorUrl.setText(sMonitorUrl);
        SpannableString sServerHost = new SpannableString(SDKInitializer.getServerHost());
        mEtServerHost.setText(sServerHost);
        SpannableString sServerScheme = new SpannableString(SDKInitializer.getServerScheme());
        mEtServerScheme.setText(sServerScheme);

        if (SDKInitializer.getApiKey() != null) {
            SpannableString sAppKey = new SpannableString(SDKInitializer.getApiKey());
            mEtAppKey.setText(sAppKey);
        }

        if (SDKInitializer.getExpandViewUrlType() == ExpandView3.UrlType.roadnet) {
            mRgExpandViewType.check(R.id.rb_roadnet);
        } else {
            mRgExpandViewType.check(R.id.rb_realview);
        }

        if (SDKInitializer.getStyleUrlType() == MineMap.UrlType.basicMap) {
            mRgMapType.check(R.id.rb_basicmap);
        } else {
            mRgMapType.check(R.id.rb_satellite);
        }

        if (SDKInitializer.getCoordType() == CoordType.GCJ02) {
            mRgCoordType.check(R.id.rb_02);
        } else {
            mRgCoordType.check(R.id.rb_84);
        }

        if (!SDKInitializer.isUseSimLocation()) {
            mRgLocationType.check(R.id.rb_gps);
        } else {
            mRgLocationType.check(R.id.rb_sim);
        }
    }

    private void clearInput() {
        mEtRouteingUrl.setText("");
        mEtTruckUrl.setText("");
        mEtExpandViewUrl.setText("");
        mEtRouteTmcUrl.setText("");
        mEtSearchUrl.setText("");
        mEtInverseUrl.setText("");
        mEtStyleUrl.setText("");
        mEtDatastoreUrl.setText("");
        mEtMonitorUrl.setText("");
        mEtServerHost.setText("");
        mEtServerScheme.setText("");
        mEtAppKey.setText("");
    }

    /**
     * @param context
     * @param key
     * @param values
     */
    private void putString(Context context, String key, String values) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit()
                .putString(key, values)
                .apply();
    }

    /**
     * @param context
     * @param key
     * @param defaultValues
     * @return
     */
    private String getString(Context context, String key, String defaultValues) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString(key, defaultValues);
    }

    /**
     * @param context
     * @param key
     * @param values
     */
    private void putInt(Context context, String key, int values) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        mSharedPreferences.edit()
                .putInt(key, values)
                .apply();
    }

    /**
     * @param context
     * @param key
     * @param defaultValues
     * @return
     */
    private int getInt(Context context, String key, int defaultValues) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        return mSharedPreferences.getInt(key, defaultValues);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.rb_basicmap:
                mMapType = 0;
                putString(mContext, "MineMap_mapType", mMapType + "");
                break;
            case R.id.rb_satellite:
                mMapType = 1;
                putString(mContext, "MineMap_mapType", mMapType + "");
                break;
            case R.id.rb_roadnet:
                mExpandViewType = 0;
                putString(mContext, "MineMap_expandViewType", mExpandViewType + "");
                break;
            case R.id.rb_realview:
                mExpandViewType = 1;
                putString(mContext, "MineMap_expandViewType", mExpandViewType + "");
                break;
            case R.id.rb_02:
                mCoordType = 0;
                putString(mContext, "MineMap_coordType", mCoordType + "");
                break;
            case R.id.rb_84:
                mCoordType = 1;
                putString(mContext, "MineMap_coordType", mCoordType + "");
                break;
            case R.id.rb_gps:
                mLocationType = 0;
                putString(mContext, "MineMap_locationType", mLocationType + "");
                break;
            case R.id.rb_sim:
                mLocationType = 1;
                putString(mContext, "MineMap_locationType", mLocationType + "");
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_generate:
                String routeingUrl = mEtRouteingUrl.getText().toString();
                String truckUrl = mEtTruckUrl.getText().toString();
                String expandViewUrl = mEtExpandViewUrl.getText().toString();
                String routeTmcUrl = mEtRouteTmcUrl.getText().toString();
                String searchUrl = mEtSearchUrl.getText().toString();
                String inverseUrl = mEtInverseUrl.getText().toString();
                String styleUrl = mEtStyleUrl.getText().toString();
                String datastoreUrl = mEtDatastoreUrl.getText().toString();
                String monitorUrl = mEtMonitorUrl.getText().toString();
                String serverHost = mEtServerHost.getText().toString();
                String serverScheme = mEtServerScheme.getText().toString();
                String appKey = mEtAppKey.getText().toString();

                if (!TextUtils.isEmpty(routeingUrl)) {
                    SDKInitializer.setRouteingUrl(routeingUrl);
                    putString(mContext, "MineMap_routeingUrl", routeingUrl);
                }

                if (!TextUtils.isEmpty(truckUrl)) {
                    SDKInitializer.setTruckUrl(truckUrl);
                    putString(mContext, "MineMap_truckUrl", truckUrl);
                }

                if (!TextUtils.isEmpty(expandViewUrl)) {
                    if (mExpandViewType == 0) {
                        SDKInitializer.setExpandViewUrl(ExpandView3.UrlType.roadnet, expandViewUrl);
                    } else {
                        SDKInitializer.setExpandViewUrl(ExpandView3.UrlType.realview, expandViewUrl);
                    }

                    putString(mContext, "MineMap_expandViewUrl", expandViewUrl);
                }

                if (!TextUtils.isEmpty(routeTmcUrl)) {
                    SDKInitializer.setRouteTmcUrl(routeTmcUrl);
                    putString(mContext, "MineMap_routeTmcUrl", routeTmcUrl);
                }

                if (!TextUtils.isEmpty(searchUrl)) {
                    SDKInitializer.setSearchUrl(searchUrl);
                    putString(mContext, "MineMap_searchUrl", searchUrl);
                }

                if (!TextUtils.isEmpty(inverseUrl)) {
                    SDKInitializer.setInverseUrl(inverseUrl);
                    putString(mContext, "MineMap_inverseUrl", inverseUrl);
                }

                if (!TextUtils.isEmpty(styleUrl)) {
                    if (mMapType == 0) {
                        SDKInitializer.setStyleUrl(MineMap.UrlType.basicMap, styleUrl);
                    } else {
                        SDKInitializer.setStyleUrl(MineMap.UrlType.satellite, styleUrl);
                    }

                    putString(mContext, "MineMap_styleUrl", styleUrl);
                }

                if (!TextUtils.isEmpty(datastoreUrl)) {
                    SDKInitializer.setDataStoreUrl(datastoreUrl);
                    putString(mContext, "MineMap_datastoreUrl", datastoreUrl);
                }

                if (!TextUtils.isEmpty(monitorUrl)) {
                    SDKInitializer.setMonitorUrl(monitorUrl);
                    putString(mContext, "MineMap_monitorUrl", monitorUrl);
                }

                if (!TextUtils.isEmpty(serverHost)) {
                    SDKInitializer.setServerHost(serverHost);
                    putString(mContext, "MineMap_serverHost", serverHost);
                }

                if (!TextUtils.isEmpty(serverScheme)) {
                    SDKInitializer.setServerScheme(serverScheme);
                    putString(mContext, "MineMap_serverScheme", serverScheme);
                }

                if (!TextUtils.isEmpty(appKey)) {
                    SDKInitializer.setApiKey(appKey);
                    putString(mContext, "MineMap_appKey", appKey);
                }

                if (mCoordType == 0) {
                    SDKInitializer.setCoordType(CoordType.GCJ02);
                } else {
                    SDKInitializer.setCoordType(CoordType.WGS84);
                }

                if (mLocationType == 0) {
                    SDKInitializer.setUseSimLocation(false);
                } else {
                    SDKInitializer.setUseSimLocation(true);
                }

                clearInput();
                setData();

                Toast.makeText(SettingActivity.this, "配置成功！", Toast.LENGTH_LONG).show();
                break;
        }
    }
}
